package Control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;

import Entity.Shop_Order;
import Form.Shop_OrderHistory_Form;

public class Shop_OrderHistory_Controller {
	
	private Shop_OrderHistory_Form parser;
	private ArrayList<Shop_Order>[] orderStack;
	
	public Shop_OrderHistory_Controller(Shop_OrderHistory_Form parser){
		this.parser = parser;
		
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){

			@Override
			protected Void doInBackground() throws Exception {
				orderStack = Shop_Order.getOrdersByCustomer(CommonControls.getUser().getNric());
				return null;
			}
			
			protected void done(){
				
				for(int i = 0; i < 3; i++){
					final ArrayList<Shop_Order> orderList = orderStack[i];
					JScrollPane scrollPane;
					if(i == 0)
						scrollPane = getParser().getReadyScrollPane();
					else if(i == 1)
						scrollPane = getParser().getPendingScrollPane();
					else
						scrollPane = getParser().getCollectedScrollPane();

					JPanel panel = new JPanel();
					panel.setPreferredSize(new Dimension(560, orderList.size() * 140));
					for(int a = 0; a < orderList.size(); a++){
						final int tempA = a;
						
						Color background;
						Color foreground;
						if(a % 2 == 0){
							background = Color.DARK_GRAY;
							foreground = Color.LIGHT_GRAY;
						}
						else{
							foreground = Color.DARK_GRAY;
							background = Color.LIGHT_GRAY;
						}
						
						JPanel holder = new JPanel();
						holder.setBackground(background);
						holder.setPreferredSize(new Dimension(540, 130));
						FlowLayout layout = (FlowLayout)holder.getLayout();
						layout.setAlignment(FlowLayout.LEFT);
						
						JLabel orderId = new JLabel(orderList.get(a).getOrderId());
						orderId.setForeground(foreground);
						orderId.setFont(new Font("Segoe UI Light", Font.BOLD, 20));
						orderId.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 20, background));
						holder.add(orderId);
						
						JLabel orderStatus = new JLabel();
						orderStatus.setOpaque(true);
						
						Color colorCode;
						Color text;
						if(i == 0){
							orderStatus.setText("Ready");
							colorCode = Color.GREEN;
							text = Color.BLACK;
						}
						else if(i == 1){
							orderStatus.setText("Pending");
							colorCode = Color.BLUE;
							text = Color.WHITE;
						}
						else{
							orderStatus.setText("Collected");
							colorCode = Color.WHITE;
							text = Color.BLACK;
						}
						orderStatus.setBackground(colorCode);
						orderStatus.setBorder(BorderFactory.createMatteBorder(5, 20, 5, 20, colorCode));
						orderStatus.setForeground(text);
						holder.add(orderStatus);
						
						JSeparator headerSplit = new JSeparator();
						headerSplit.setForeground(foreground);
						headerSplit.setPreferredSize(new Dimension(530, 5));
						holder.add(headerSplit);
						
						JPanel datesHolder = new JPanel();
						datesHolder.setBackground(foreground);
						datesHolder.setPreferredSize(new Dimension(300, 50));
						layout = (FlowLayout)datesHolder.getLayout();
						layout.setAlignment(FlowLayout.LEFT);
						
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
						
						JLabel orderDate = new JLabel("Ordered on: " + sdf.format(orderList.get(a).getOrderDate()));
						orderDate.setFont(new Font("Segoe UI Light", Font.BOLD, 12));
						orderDate.setForeground(background);
						datesHolder.add(orderDate);
						
						JLabel collectDate = new JLabel("Collection date(Tentative): " + sdf.format(orderList.get(a).getCollectDate()));
						collectDate.setFont(new Font("Segoe UI Light", Font.BOLD, 12));
						collectDate.setForeground(background);
						datesHolder.add(collectDate);
						
						holder.add(datesHolder);
						
						JSeparator split = new JSeparator();
						split.setForeground(foreground);
						split.setOrientation(SwingConstants.VERTICAL);
						split.setPreferredSize(new Dimension(5, 50));
						holder.add(split);
						
						JPanel ordersHolder = new JPanel();
						ordersHolder.setOpaque(false);
						ordersHolder.setPreferredSize(new Dimension(200, 50));
						layout = (FlowLayout)ordersHolder.getLayout();
						layout.setAlignment(FlowLayout.LEFT);
						
						JLabel ordersTxt = new JLabel("Orders:");
						ordersTxt.setForeground(foreground);
						ordersTxt.setFont(new Font("Segoe UI Light", Font.BOLD, 15));
						ordersHolder.add(ordersTxt);
						
						final JComboBox orders = new JComboBox();
						orders.setPreferredSize(new Dimension(100, 20));
						for(int b = 0; b < orderList.get(a).getCustomizations().size(); b++){
							orders.addItem(orderList.get(a).getCustomizations().get(b).getCustId());
						}
						orders.setSelectedIndex(0);
						orders.addActionListener(new ActionListener(){

							@Override
							public void actionPerformed(ActionEvent arg0) {
								JDialog preview = new JDialog(getParser().getParser(), true);
								JTabbedPane previewTabs = new JTabbedPane();
								
								JLabel front = new JLabel();
								front.setPreferredSize(new Dimension(300, 400));
								front.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(orderList.get(tempA).getCustomizations().get(orders.getSelectedIndex()).getFrontCustUrl()).getScaledInstance(300, 400, Image.SCALE_DEFAULT)));
								previewTabs.addTab("Front", front);
								
								JLabel back = new JLabel();
								back.setPreferredSize(new Dimension(300, 400));
								back.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(orderList.get(tempA).getCustomizations().get(orders.getSelectedIndex()).getBackCustUrl()).getScaledInstance(300, 400, Image.SCALE_DEFAULT)));
								previewTabs.addTab("Back", back);
								
								preview.add(previewTabs);
								
								preview.pack();
								preview.setLocationRelativeTo(getParser().getParser());
								preview.setVisible(true);
							}
							
						});
						ordersHolder.add(orders);
						
						JLabel totalPrice = new JLabel();
						totalPrice.setText("Total price: $" + orderList.get(a).getTotalPrice());
						totalPrice.setForeground(foreground);
						totalPrice.setFont(new Font("Segoe UI Light", Font.BOLD, 15));
						ordersHolder.add(totalPrice);
						
						holder.add(ordersHolder);
						
						panel.add(holder);
					}
					scrollPane.getViewport().add(panel);
				}
				getParser().getParser().closeProgressBar();
				
			}
			
		};
		getParser().getParser().getProgressBar();
		worker.execute();
		
	}

	public Shop_OrderHistory_Form getParser() {
		return parser;
	}

	public void setParser(Shop_OrderHistory_Form parser) {
		this.parser = parser;
	}

	public ArrayList<Shop_Order>[] getOrderStack() {
		return orderStack;
	}

	public void setOrderStack(ArrayList<Shop_Order>[] orderStack) {
		this.orderStack = orderStack;
	}

}
